package com.station.search.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.station.search.common.domain.WebResponse;
import com.station.search.common.domain.WebResponseBuilder;
import com.station.search.constant.DeleteEnum;
import com.station.search.domain.StationRepairRecord;
import com.station.search.domain.converter.StationRepairRecordConverter;
import com.station.search.domain.vo.StationRepairRecordVO;
import com.station.search.service.StationRepairRecordService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author: JT
 * @date: 2021/11/21
 * @Title:
 */
@RestController
@RequestMapping
public class StationRepairRecordController {
    @Resource
    private StationRepairRecordService stationRepairRecordService;

    @GetMapping("/stations/repairRecords")
    public WebResponse repairRecords(
            @RequestParam(value="stationCode", required = false) String stationCode,
            @RequestParam(value="searchWord", required = false) String searchWord
    ) {
        LambdaQueryChainWrapper<StationRepairRecord> stationRepairRecordsQuery = stationRepairRecordService.lambdaQuery();
        stationRepairRecordsQuery.eq(StationRepairRecord::getIsDelete, DeleteEnum.NOT_DELETED.getValue());
        if (StringUtils.hasText(stationCode)) {
            stationRepairRecordsQuery.eq(StationRepairRecord::getStationCode, stationCode);
        }
        if (StringUtils.hasText(searchWord)) {
            stationRepairRecordsQuery.and(
                    query -> query.like(StationRepairRecord::getStationCode, searchWord)
                            .or()
                            .like(StationRepairRecord::getStationName, searchWord)
                            .or()
                            .like(StationRepairRecord::getRepairRecord, searchWord)
            );
        }
        stationRepairRecordsQuery.orderByDesc(StationRepairRecord::getRepairDate);
        List<StationRepairRecord> stationRepairRecords = stationRepairRecordsQuery.list();
        return WebResponseBuilder.ok("获取抢修记录列表")
                .data(
                        stationRepairRecords.stream()
                                .map((record) -> StationRepairRecordConverter.INSTANCE.doToDto(record))
                                .map(record -> {
                                    if(Objects.isNull(record.getRepairRecord())) {
                                        return record;
                                    }
                                    record.setRepairRecord(record.getRepairRecord().replace("\n", ""));
                                    if (record.getRepairRecord().length() <= 10) {
                                        return record;
                                    }
                                    record.setRepairRecord(record.getRepairRecord().substring(0, 10) + "...");
                                    return record;
                                })
                                .collect(Collectors.toList())

                );
    }

    @PostMapping("/stations/repairRecords")
    public WebResponse addRepairRecord(@RequestBody StationRepairRecordVO stationRepairRecordVO) {
        StationRepairRecord stationRepairRecord = StationRepairRecordConverter.INSTANCE.voToDo(stationRepairRecordVO);
        stationRepairRecord.setRepairPeople(
                stationRepairRecord.getRepairPeople()
                        .stream()
                        .filter(item -> StringUtils.hasText(item.getText()))
                        .collect(Collectors.toList())
        );
        stationRepairRecordService.saveOrUpdate(stationRepairRecord);
        if (StringUtils.isEmpty(stationRepairRecordVO.getId())) {
            return WebResponseBuilder.ok("添加台站[{}]抢修记录[{}]成功！", stationRepairRecord.getStationCode(), stationRepairRecord.getId());
        }
        return WebResponseBuilder.ok("修正台站[{}]巡检记录[{}]成功！", stationRepairRecord.getStationCode(), stationRepairRecord.getId());
    }

    @GetMapping("/stations/repairRecords/{recordId}")
    public WebResponse repairRecordById(@PathVariable("recordId") String recordId) {
        StationRepairRecord stationRepairRecordQuery = new StationRepairRecord();
        stationRepairRecordQuery.setId(recordId);
        StationRepairRecord stationRepairRecord = stationRepairRecordService.getOne(new QueryWrapper<>(stationRepairRecordQuery));
        return WebResponseBuilder.ok(
                "获取台站[{}/{}]抢修记录[{}]信息!", stationRepairRecord.getStationCode(), stationRepairRecord.getStationName(), stationRepairRecord.getId()
        ).data(
                StationRepairRecordConverter.INSTANCE.doToDto(stationRepairRecord)
        );
    }
}
